{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Querying Multiple Workflows (Lattices)\n",
    "\n",
    "Query the results of multiple workflows (lattices) run in the same notebook.\n",
    "\n",
    "### Prerequisites\n",
    "\n",
    "Construct mutliple workflows (or a single workflow to be dispatched multiple times)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import covalent as ct\n",
    "\n",
    "@ct.electron\n",
    "def identity(x):\n",
    "    return x\n",
    "\n",
    "@ct.electron\n",
    "def square(x):\n",
    "    return x * x\n",
    "\n",
    "@ct.lattice\n",
    "def workflow(a):\n",
    "    val_1 = identity(x=a)\n",
    "    return square(x=val_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Procedure\n",
    "\n",
    "1. Dispatch the workflows. Record the dispatch IDs to enable retrieval of the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8a42ce81-62c6-44a5-bbdf-ecbfd99cf87e\n",
      "46877bbb-28c4-4227-9ca0-f1c27bc2293c\n",
      "15307ee7-ecc5-484d-9cfb-4a39cc9b1d55\n",
      "7d64ef14-28ac-49b5-8867-1bfea93d73d5\n"
     ]
    }
   ],
   "source": [
    "params = [1, 2, 3, 4]\n",
    "dispatch_ids = [ct.dispatch(workflow)(a=param) for param in params]\n",
    "\n",
    "for dispatch_id in dispatch_ids:\n",
    "    print(dispatch_id)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Query and retrieve the workflow results corresponding to the dispatch IDs:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = []\n",
    "\n",
    "for dispatch_id in dispatch_ids:\n",
    "    result = ct.get_result(dispatch_id=dispatch_id, wait=True)\n",
    "    results.append(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Use the `print()` method to see the results of individual workflows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Lattice Result\n",
      "==============\n",
      "status: COMPLETED\n",
      "result: 1\n",
      "input args: []\n",
      "input kwargs: {'a': '1'}\n",
      "error: None\n",
      "\n",
      "start_time: 2023-02-01 19:40:08.641600\n",
      "end_time: 2023-02-01 19:40:08.976987\n",
      "\n",
      "results_dir: /Users/mini-me/agnostiq/covalent/doc/source/how_to/collection/results\n",
      "dispatch_id: 8a42ce81-62c6-44a5-bbdf-ecbfd99cf87e\n",
      "\n",
      "Node Outputs\n",
      "------------\n",
      "identity(0): 1\n",
      ":parameter:1(1): 1\n",
      "square(2): 1\n",
      "\n",
      "\n",
      "Lattice Result\n",
      "==============\n",
      "status: COMPLETED\n",
      "result: 4\n",
      "input args: []\n",
      "input kwargs: {'a': '2'}\n",
      "error: None\n",
      "\n",
      "start_time: 2023-02-01 19:40:08.667773\n",
      "end_time: 2023-02-01 19:40:09.001117\n",
      "\n",
      "results_dir: /Users/mini-me/agnostiq/covalent/doc/source/how_to/collection/results\n",
      "dispatch_id: 46877bbb-28c4-4227-9ca0-f1c27bc2293c\n",
      "\n",
      "Node Outputs\n",
      "------------\n",
      "identity(0): 2\n",
      ":parameter:2(1): 2\n",
      "square(2): 4\n",
      "\n",
      "\n",
      "Lattice Result\n",
      "==============\n",
      "status: COMPLETED\n",
      "result: 9\n",
      "input args: []\n",
      "input kwargs: {'a': '3'}\n",
      "error: None\n",
      "\n",
      "start_time: 2023-02-01 19:40:08.697165\n",
      "end_time: 2023-02-01 19:40:08.994736\n",
      "\n",
      "results_dir: /Users/mini-me/agnostiq/covalent/doc/source/how_to/collection/results\n",
      "dispatch_id: 15307ee7-ecc5-484d-9cfb-4a39cc9b1d55\n",
      "\n",
      "Node Outputs\n",
      "------------\n",
      "identity(0): 3\n",
      ":parameter:3(1): 3\n",
      "square(2): 9\n",
      "\n",
      "\n",
      "Lattice Result\n",
      "==============\n",
      "status: COMPLETED\n",
      "result: 16\n",
      "input args: []\n",
      "input kwargs: {'a': '4'}\n",
      "error: None\n",
      "\n",
      "start_time: 2023-02-01 19:40:08.724797\n",
      "end_time: 2023-02-01 19:40:09.026908\n",
      "\n",
      "results_dir: /Users/mini-me/agnostiq/covalent/doc/source/how_to/collection/results\n",
      "dispatch_id: 7d64ef14-28ac-49b5-8867-1bfea93d73d5\n",
      "\n",
      "Node Outputs\n",
      "------------\n",
      "identity(0): 4\n",
      ":parameter:4(1): 4\n",
      "square(2): 16\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for i in (params):\n",
    "    print(results[i-1])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### See Also\n",
    "\n",
    "[Executing a Workflow (Lattice) Multiple Times](../execution/execute_lattice_multiple_times.ipynb)\n",
    "\n",
    "[Executing Multiple Workflows (Lattices)](../execution/execute_multiple_lattices.ipynb)\n",
    "\n",
    "[Querying Workflow (Lattice) Execution Status in a Notebook](../status/query_lattice_execution_status.ipynb)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "f63e7985caea997504032bc67ebf6b6ff07db5420026db9c4de4a9a029ce747c"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
